local addon, ns = ...
local L = ns.L

local Broker_EverythingDB = _G["Broker_EverythingDB"]


local BEModules = {
	[L["Durability"]] = {
		["Desc"] = L["Broker to show durability of your gear and estimated repair total."],
		["Label"] = L["Durability"],
	},
	[L["Guild"]] = {
		["Desc"] = L["Broker to show guild information. Guild members currently online, MOTD, guild xp etc."],
		["Label"] = L["Guild"],
	},
	[L["Calendar"]] = {
		["Desc"] = L["Broker to show if you have any current invitations."],
		["Label"] = L["Calendar"],
	},
	[L["Bags"]] = {
		["Desc"] = L["Broker to show total, free and filled amount of bag space."],
		["Label"] = L["Bags"],
	},
	[L["Equipment"]] = {
		["Desc"] = L["Broker to show your Equipment Sets. Also allows deleting, equiping and saving of sets."],
		["Label"] = L["Equipment"],
	},
	[L["Zonetext"]] = {
		["Desc"] = L["Broker to show the name of the current zone."],
		["Label"] = L["Zone Text"],
	},
	[L["Gold"]] = {
		["Desc"] = L["Broker to show gold information. Shows gold amounts for characters on the same realm and faction and the amount made or lost for the session."],
		["Label"] = L["Gold"],
	},
	[L["Clock"]] = {
		["Desc"] = L["Broker to show realm or local time."],
		["Label"] = L["Clock"],
	},
	[L["GPS"]] = {
		["Desc"] = L["Broker to show the name of the current Zone and the co-ordinates."],
		["Label"] = L["GPS"],
	},
	[L["Latency"]] = {
		["Desc"] = L["Broker to show your current latency. Can be configured to show both Home and/or World latency."],
		["Label"] = L["Latency"],
	},
	[L["Dualspec"]] = {
		["Desc"] = L["Broker to show your currently selected spec. If you have a second spec, it is shown in the tool tip, and can be swapped to using this broker."],
		["Label"] = L["Dual Spec"],
	},
	[L["XP"]] = {
		["Desc"] = L["Broker to show your xp. Can be shown either as a percentage, or as values."],
		["Label"] = L["XP"],
	},
	[L["Stuff"]] = {
		["Desc"] = L["Broker to allow you to do...Stuff! Switch to windowed mode, reload ui, logout and quit."],
		["Label"] = L["Stuff"],
	},
	[L["FPS"]] = {
		["Desc"] = L["Broker to show your frames per second."],
		["Label"] = L["FPS"],
	},
	[L["Friends"]] = {
		["Desc"] = L["Broker to show you which friends are online."],
		["Label"] = L["Friends"],
	},
	[L["Memory"]] = {
		["Desc"] = L["Broker to show you your addon memory usage."],
		["Label"] = L["Memory"],
	},
	[L["Mail"]] = {
		["Desc"] = L["Broker to alert you if you have mail."],
		["Label"] = L["Mail"],
	},
	[L["Nameplates"]] = {
		["Desc"] = L["Broker to allow you to toggle the various nameplates. Eg, friendly or hostile."],
		["Label"] = L["Nameplates"],
	},
	[L["Location"]] = {
		["Desc"] = L["Broker to show your current co-ordinates within the zone."],
		["Label"] = L["Location"],
	},
	[L["Tracking"]] = {
		["Desc"] = L["Broker to show what you are currently tracking. You can also change the tracking types from this broker."],
		["Label"] = L["Tracking"],
	},
}

local function BrokerPanel(panel)
	local controls = {}	
    local function makeToggle(name, desc, varname)
        return panel:MakeToggle(
            "name", name, 
            "description", desc, 
            "default", Broker_EverythingDB[varname].enabled, 
            "getFunc", function() return Broker_EverythingDB[varname].enabled end, 
            "setFunc", function(value) Broker_EverythingDB[varname].enabled = value end
        )
    end

	panel.title, panel.subText = panel:MakeTitleTextAndSubText(
		L["Broker: Everything  - Brokers"], L["Select the listed broker to enable/disable. You must Reload UI for any changes to apply."])

	for k, v in pairs(BEModules) do 
		panel[k] = makeToggle(BEModules[k].Label, BEModules[k].Desc, k)
		table.insert(controls, panel[k])
	end

	panel.reload = panel:MakeButton(
	    'name', L['Reload UI'],
	    'description', L['Reloads the UI. You must do this to apply any changes in module activation.'],
	    'func', function() ReloadUI() end
	)		
    table.insert(controls, panel.reload)

    for i, frame in ipairs(controls) do
        if i == 1 then
            frame:SetPoint("TOPLEFT", panel.subText, "BOTTOMLEFT", 0, -10)
        elseif i == 8 then
			frame:SetPoint("TOPLEFT", panel.subText, "BOTTOMLEFT", 200, -10)
		elseif i == 15 then 
			frame:SetPoint("TOPLEFT", panel.subText, "BOTTOMLEFT", 400, -10)
		else
            frame:SetPoint("TOPLEFT", controls[i-1], "BOTTOMLEFT", 0, -5)
        end
    end
	
	
	controls[#controls]:SetPoint("TOPLEFT", controls[7], "BOTTOMLEFT", 0, -20)


end


local function OptionsPanel(panel)
	local controls = {}	
    local function makeCheck(name, desc, module, option)
        return panel:MakeToggle(
            "name", name, 
            "description", desc,
            "default", false, 
            "getFunc", function() if module == "nil" or module == nil then return Broker_EverythingDB[option] else return Broker_EverythingDB[module][option] end end, 
            "setFunc", function(value) if module == "nil" or module == nil then Broker_EverythingDB[option] = value else Broker_EverythingDB[module][option] = value end end
        )
    end

	local function getTitle(text, tType)
		local title
		if tType == "group" then
			title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalMed3")
		else
			title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		end
		title:SetText(text)
		title:SetJustifyH("LEFT")
		title:SetJustifyV("CENTER")
		title:SetHeight(20)
		return title
	end

	local function getDivider()
		local div = CreateFrame("Frame", nil, panel)
		div:CreateTexture(nil, "ARTWORK", "videoUnderline")
		div:SetSize(panel:GetParent():GetWidth() - 50, 2)
		return div
	end
	
	
	panel.title, panel.subText = panel:MakeTitleTextAndSubText(
		L["Broker: Everything  - Options"], L["Allows you to adjust the display options."])
	
	panel.generalTitle = getTitle(L["General Options"], "group")
	panel.generalDivide = getDivider()
	panel.suffixColoring = makeCheck(L["Suffix Colouring"], L["Enable/Disable Class colouring of the information display suffixes. (eg, ms, fps etc)"], "nil", "suffixColour")
	panel.showLabels = makeCheck(L["Show Broker Labels"], L["Enable/Disable the showing of the data broker labels."], "nil", "labels")
	panel.global = panel:MakeToggle(
            "name", L["Use a global profile"], 
            "description", L["Enable/Disable the showing of a global Broker_Everything profile across all of your characters."],
            "default", false, 
            "getFunc", function() return Broker_EverythingDB.global end, 
            "setFunc", function(value) 
            	if value == true and Broker_EverythingGlobalDB[L["Clock"]] == nil then
					Broker_EverythingGlobalDB = Broker_EverythingDB
				end
				Broker_EverythingGlobalDB.global = value
        	end
        )
	
	panel.moduleOptionsTitle = getTitle(L["Module Options"], "group")
	panel.moduleDivide = getDivider()

	panel.latencyTitle = getTitle(L["Latency"])
	panel.latencyHome = makeCheck(L["Home Latency"], L["Enable/Disable the display of the latency to the home realm"], L["Latency"], "showHome")
	panel.latencyWorld = makeCheck(L["World Latency"], L["Enable/Disable the display of the latency to the world realms"], L["Latency"], "showWorld")
	
	panel.clockTitle = getTitle(L["Clock"])
	panel.clock24 = makeCheck(L["24 Hour Time"], L["Enable to display the time in 24 hour format; Disable to show as AM/PM"], L["Clock"], "format24")
	panel.clockLocal = makeCheck(L["Show Local Time"], L["Enable to display the local time. Disable to show the realm time."], L["Clock"], "timeLocal")
	
	panel.guildTitle = getTitle(L["Guild"])
	panel.guildXP = makeCheck(L["Show Guild XP"], L["Enable/Disable the display of Guild XP in the Guild data broker tooltip."], L["Guild"], "showXP")
	
	panel.XPTitle = getTitle(L["XP"])
	panel.XP = makeCheck(L["Display XP as Absolute Value"], L["Select to show XP as an absolute value; Deselected will show it as a percentage."], L["XP"], "display")

	panel.mailTitle = getTitle(L["Mail"])
	panel.mailPlaysound = makeCheck(L["Play sound on new mail"], L["Enable to play a sound on receiving a new mail message. Default is off"], L["Mail"], "playsound")
	
	panel.addonsTitle = getTitle(L["Memory"])
	panel.memory = panel:MakeSlider(
	    'name', "",
	    'description', L["Select the maximum number of addons to display, otherwise drag to 'All'."],
	    'minText', L['All'],
	    'maxText', '100',
	    'minValue', -1,
	    'maxValue', 100,
	    'step', 1,
	    'default', -1,
	    'current', Broker_EverythingDB[L["Memory"]].mem_max_addons,
	    'setFunc', function(value) Broker_EverythingDB[L["Memory"]].mem_max_addons = value end,
		'currentTextFunc', function(value) return ("%.0f"):format(value) end
	)

	panel.reset = panel:MakeButton(
	    'name', L['Reset'],
	    'description', L['Resets the Broker_Everything Defaults and Reloads the UI.'],
	    'func', function() Broker_EverythingDB.reset = true ReloadUI() end
	)
	
	
	panel.generalTitle:SetPoint("TOPLEFT", panel.subText, "BOTTOMLEFT", 0, -10)
	panel.generalDivide:SetPoint("TOPLEFT", panel.generalTitle, "BOTTOMLEFT", 0, 0)
	panel.suffixColoring:SetPoint("TOPLEFT", panel.generalTitle, "BOTTOMLEFT", 0, -5)
	panel.showLabels:SetPoint("TOPLEFT", panel.suffixColoring, "TOPLEFT", 150, 0)
	panel.global:SetPoint("TOPLEFT", panel.generalDivide, "TOPLEFT", 325, -5)
	
	panel.moduleOptionsTitle:SetPoint("TOPLEFT", panel.suffixColoring, "BOTTOMLEFT", 0, -10)

	panel.moduleDivide:SetPoint("TOPLEFT", panel.moduleOptionsTitle, "BOTTOMLEFT", 0, 0)
	
	panel.latencyTitle:SetPoint("TOPLEFT", panel.moduleDivide, "BOTTOMLEFT", 0, -5)
	panel.latencyHome:SetPoint("TOPLEFT", panel.latencyTitle, "BOTTOMLEFT", 0, -5)
	panel.latencyWorld:SetPoint("TOPLEFT", panel.latencyHome, "TOPLEFT", 150, 0)
	
	panel.clockTitle:SetPoint("TOPLEFT", panel.moduleDivide, "BOTTOMLEFT", 325, -5)
	panel.clock24:SetPoint("TOPLEFT", panel.clockTitle, "BOTTOMLEFT", 0, -5)
	panel.clockLocal:SetPoint("TOPLEFT", panel.clock24, "TOPLEFT", 150, 0)
	
	panel.guildTitle:SetPoint("TOPLEFT", panel.latencyTitle, "BOTTOMLEFT", 0, -40)
	panel.guildXP:SetPoint("TOPLEFT", panel.guildTitle, "BOTTOMLEFT", 0, -5)
	
	panel.XPTitle:SetPoint("TOPLEFT", panel.guildTitle, "TOPLEFT", 325, 0)
	panel.XP:SetPoint("TOPLEFT", panel.XPTitle, "BOTTOMLEFT", 0, -5)

	panel.mailTitle:SetPoint("TOPLEFT", panel.guildTitle, "BOTTOMLEFT", 0, -40)
	panel.mailPlaysound:SetPoint("TOPLEFT", panel.mailTitle, "BOTTOMLEFT", 0, -5)


	
	panel.addonsTitle:SetPoint("TOPLEFT", panel.mailTitle, "BOTTOMLEFT", 0, -40)
	panel.memory:SetPoint("TOPLEFT", panel.addonsTitle, "BOTTOMLEFT", 0, -5)

	panel.reset:SetPoint("TOPLEFT", panel.addonsTitle, "BOTTOMLEFT", 0, -60)
	
	
end

local Broker_EverythingConfig = CreateFrame("Frame")
Broker_EverythingConfig:RegisterEvent("ADDON_LOADED")

Broker_EverythingConfig:SetScript("OnEvent", function(self, event, ...)
	if IsAddOnLoaded("Broker_Everything") then
		local LSO = LibStub("LibSimpleOptions-1.0")

		-- Main option panel
		local brokerPanel = LSO.AddOptionsPanel("Broker:Everything", BrokerPanel)
		local optionsPanel = LSO.AddSuboptionsPanel("Broker:Everything", L["Options"], OptionsPanel)

		Broker_EverythingConfig:UnregisterEvent("ADDON_LOADED")
	end
end)